window.onload = function() {
    if (parent != window) {
        parent.location.href = "/admin/login";
    }
    layui.use(['form', 'layer', 'jquery'], function() {
        var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer
        $ = layui.jquery;

        $(".loginBody .seraph").click(function() {
            Utils.layer.msg("这只是做个样式，至于功能，你见过哪个后台能这样登录的？还是老老实实的找管理员去注册吧", {
                time: 5000
            });
        })

        //登录按钮
        form.on("submit(login)", function(data) {
            $(this).text("登录中...").attr("disabled", "disabled").addClass("layui-disabled");
            //登录
            login();
            return false;

        });

        function login() {
            var form = $('#form1');

            $.ajax({
                'type': 'POST',
                'url': '/admin/login',
                'data': form.serialize(),
                'dataType': 'json',
                'beforeSend': function() {
                    Utils.layer.load(1, {
                        shade: [0.1, '#fff'] //0.1透明度的白色背景
                    });
                },
                'success': function(data) {
                    Utils.layer.closeAll();
                    if (data.code != 200) {
                        Utils.layer.msg(data.msg)
                        $('.login_btn').text("登录").removeAttr("disabled").removeClass("layui-disabled")
                        return false;
                    }
                    location.href = '/';
                }
            });
        }

        //表单输入效果
        $(".loginBody .input-item").click(function(e) {
            e.stopPropagation();
            $(this).addClass("layui-input-focus").find(".layui-input").focus();
        })
        $(".loginBody .layui-form-item .layui-input").focus(function() {
            $(this).parent().addClass("layui-input-focus");
        })
        $(".loginBody .layui-form-item .layui-input").blur(function() {
            $(this).parent().removeClass("layui-input-focus");
            if ($(this).val() != '') {
                $(this).parent().addClass("layui-input-active");
            } else {
                $(this).parent().removeClass("layui-input-active");
            }
        })

        // 查看进度
        $('.look-progress').on('click', function() {
            Utils.layer.open({
                type: 1,
                title: '输入管理员电话，查看进度',
                content: "<div class='b-div' style='width:90%; margin:0 auto;'><p class='b-p' style='font-size:15px; color:#999; margin:10px 0;'>请输入管理员电话</p><input type='text' class='layui-input b-text'><p class='b-p' style='font-size:15px; color:#999; margin:10px 0;'>请输入验证码</p><input type='text' class='layui-input b-code' style='width: 56%; display: inline-block;'> <input type='button' class='layui-btn getchecknumse' style='width: 40%; float:right' value='获取验证码'><div class='b-btn'></div></div>",
                btn: ['确认', '取消'],
                btn1: function() {
                    var phone = $('.b-text').val();
                    var code = $('.b-code').val();
                    if (phone == '') {
                        Utils.layer.msg('电话号码必填');
                        return
                    } else if (code == '') {
                        Utils.layer.msg('请输入验证码');
                        return
                    } else {
                        $.ajax({
                            'type':'POST',
                            'url':'/shop/get-info',
                            'data':{
                                admin_phone:phone,
                                code:code
                            },
                            'dataType':'json',
                            'success':function(data){
                                Utils.layer.msg(data.msg);
                                if(data.code == 200){
                                    setTimeout(function() {
                                        location.href = '/shop/update-shop?admin_phone=' + phone;
                                    }, 1200);
                                }
                            }
                        });
                        
                    }
                },
                btn2: function() { }
            })
            $('.getchecknumse').click(function() {
                var phone = $('.b-text').val();
                getCheckNum(phone, ".getchecknumse");
            })
        });
        // 获取验证码
        function getCheckNum(phoneNum, CheckNumBtn) {
            var phone_num = phoneNum;
            if (/^[1][3,4,5,7,8][0-9]{9}$/.test(phone_num)) {
                $.post("/code/send-code", { phone: phone_num }, function success(data) {
                    if (data.code == 200) {
                        Utils.layer.msg(data.msg);
                        var second = 90;
                        var timer = setInterval(function() {
                            second--;
                            if (second != 0) {
                                $(CheckNumBtn).css({ "background": "#999", "color": "#eee" }).val("重新发送" + second + "s");
                                $(CheckNumBtn).attr("disabled", "disabled");
                            } else {
                                $(CheckNumBtn).val("获取验证码").css({ "background": "#08c", "color": "#fff" });
                                $(CheckNumBtn).removeAttr("disabled");
                                clearInterval(timer);
                            }
                        }, 1000);
                    } else { Utils.layer.msg(data.msg); }
                });
            } else {
                Utils.layer.msg("您输入的电话有误，请重新输入")
            }
        }
    })

}
